var num = 60;
var int;

function checkCode() {
    var url = baseQuestionnaireUrl + "phone/" + $("#phone").val() + "/code/" + $("#code").val() + "/auth";
    $.ajax({
        url: url,
        contentType: "application/json",
        type: "GET",
        success: function (data) {
            submitResult();
        }
    });
}

function submitResult() {
    var href = window.location.search;
    var params = parseUrl(href);
    var url = baseQuestionnaireUrl + "add/phone?resultId=" + params[0].value + "&phone=" + $("#phone").val();
    $.ajax({
        url: url,
        contentType: "application/json",
        type: "GET",
        success: function (data) {
            window.location.href = baseQuestionnaireUrl + "result/" + params[0].value + "/pic";
        }
    });
}

/**
 * 发送验证码
 */
function sendCode() {
    if ($("#sendCode").text() === "发送验证码") {
        int = setInterval("countdown()", 1000);
        var url = baseQuestionnaireUrl + "phone/" + $("#phone").val() + "/code";
        $.ajax({
            url: url,
            type: "GET",
            success: function (data) {
                // alert("验证码发送成功");
            }
        });
    }
}

function getPic(resultId) {
    var url = baseQuestionnaireUrl + "result/" + resultId + "/pic";
    $.ajax({
        url: url,
        type: "GET",
        success: function (data) {
            alert("验证码发送成功");
        }
    });
}

/**
 * 倒计时发送验证码的事件
 */
function countdown() {
    if (num >= 0) {
        var time = num--;
        $("#sendCode").text(time + "s");
    } else {
        num = 60;
        $("#sendCode").text("发送验证码");
        window.clearInterval(int);
    }
}